package cn.stronglink.asset.module.dam.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.stronglink.asset.model.CabinetExt;
import cn.stronglink.asset.model.DeviceType;
import cn.stronglink.asset.model.SensorSetting;
import cn.stronglink.asset.module.assetmanage.cabinet.mapper.CabinetExtMapper;
import cn.stronglink.asset.module.dam.entity.dev.RackInfo;
import cn.stronglink.asset.module.dam.entity.message.MsgRealTimeDatas;
import cn.stronglink.asset.module.dam.interfaces.RealtimeDatasHandleStrategyI;
import cn.stronglink.asset.module.dam.mapper.DamSensorMapper;
import cn.stronglink.asset.module.dam.utils.dataJsonUtils.realtime.receive.IntercepterRealTimeDatasUtil;
import cn.stronglink.asset.module.dam.utils.dataJsonUtils.realtime.receive.ListContainerUtil;
import cn.stronglink.asset.module.devicetype.mapper.DeviceTypeMapper;

@Service("toDoorDbService")
public class RealTimeSendToDoorDb implements RealtimeDatasHandleStrategyI {
	// 共6个传感器实时数据 + 2个list存放sensro温湿度 + 2个list存放cabinet门数据
	private ListContainerUtil listContainer = new ListContainerUtil();

	@Autowired
	private CabinetExtMapper cabinetExtMapper;// 门状态
	@Autowired
	private DeviceTypeMapper deviceTypeMapper;// 心跳时间
	@Autowired
	private DamSensorMapper damSensorMapper;

	// 实体类
	private CabinetExt cabinetExt = new CabinetExt();
	private DeviceType deviceType = new DeviceType();

	@Override
	public void handleDamDatas(MsgRealTimeDatas realDatas) {
		listContainer.clearAllDoorSensor();// 清空容器
//		long now = System.currentTimeMillis();

		if (IntercepterRealTimeDatasUtil.doorDiff < 1000) {
			return;// 未到1s
		}

		SensorSetting[] senList = realDatas.getAwsPostdata().getSensorList();
		String rackConverCode = realDatas.getAwsPostdata().getRackConverCode();
		boolean cabUp = false;
		boolean cabIn = false;
		boolean devUp = false;
		boolean devIn = false;

		// 查询传感器信息： 是否已存在
		synchronized (this) {
			RackInfo rackinfo = damSensorMapper.getRackInfo(rackConverCode);
			if (rackinfo==null || rackinfo.getRackId()==null) {
				System.out.println("查询..无此机柜, 无效的rackConverCode");
				return;
			}

			// 存在该机柜,更新数据
			long rackId = rackinfo.getRackId();

			for (SensorSetting sen : senList) {
				// 门数据
				cabinetExt.setAssetId(rackId);
				cabinetExt.setDoorStatus(sen.getSensorDatas());
				System.out.println("门id数量--------" + rackinfo.getCabinetIdCount());
				if (rackinfo.getCabinetIdCount() > 0) {// 存在,更新
					cabUp = true;
					listContainer.getCabUplist().add(cabinetExt);
				} else {// 不存在,插入
					cabIn = true;
					cabinetExt.setCreateTime(new Date());
					listContainer.getCabInlist().add(cabinetExt);
				}

				// 心跳时间
				deviceType.setAssetId(rackId);
				deviceType.setOnlineTime(new Date());
				if (rackinfo.getDeviceTypeIdCount() > 0) {// 存在,更新
					devUp = true;
					listContainer.getDevtypeUplist().add(deviceType);
				} else {// 不存在,插入
					devIn = true;
					cabinetExt.setCreateTime(new Date());
					listContainer.getDevtypeInlist().add(deviceType);
				}
			} // for结束

			// 批量更新或增加 门数据 cabExt表
			if (cabIn) {// cab表： 插入
				cabinetExtMapper.insertCabinetExtDoorStatus(listContainer.getCabInlist());
				System.out.println("插入cab结束。。。。。。\n");
			}
			if (cabUp) {
				cabinetExtMapper.updateCabinetExtDoorStatus(listContainer.getCabUplist());
				System.out.println("更新cab结束。。。。。。\n");
			}

			// 批量更新或增加 心跳时间:deviceType表
			if (devIn) {// cab表： 插入
				deviceTypeMapper.insertDeviceTypeHeartBeat(listContainer.getDevtypeInlist());
				System.out.println("插入dev结束。。。。。。\n");
			}
			if (devUp) {
				deviceTypeMapper.updateDeviceTypeHeartBeat(listContainer.getDevtypeUplist());
				System.out.println("更新dev结束。。。。。。\n");
			}
		}

	} // 存在该机柜

	@Override
	public boolean isHandle(MsgRealTimeDatas realDatas) {
		SensorSetting[] senArr = realDatas.getAwsPostdata().getSensorList();

		for (SensorSetting sen : senArr) {
			if (sen.getSensorType() == 3) {// 判断温湿度表
				return true;
			}
		}
		return false;
	}
}
